package com.aruba.filter;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * MaskFilter 模糊滤镜效果
 */
public class MaskFilterView extends View {

    public MaskFilterView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        RectF rectF = new RectF(50, 50, 350, 350);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);

        canvas.drawRect(rectF, paint);

        MaskFilter maskFilter = new BlurMaskFilter(50, BlurMaskFilter.Blur.NORMAL);
        paint.setMaskFilter(maskFilter);

        canvas.translate(400, 0);
        canvas.drawRect(rectF, paint);

        maskFilter = new BlurMaskFilter(50, BlurMaskFilter.Blur.SOLID);
        paint.setMaskFilter(maskFilter);

        canvas.translate(-400, 400);
        canvas.drawRect(rectF, paint);

        maskFilter = new BlurMaskFilter(50, BlurMaskFilter.Blur.OUTER);
        paint.setMaskFilter(maskFilter);

        canvas.translate(400, 0);
        canvas.drawRect(rectF, paint);

        maskFilter = new BlurMaskFilter(50, BlurMaskFilter.Blur.INNER);
        paint.setMaskFilter(maskFilter);

        canvas.translate(-400, 400);
        canvas.drawRect(rectF, paint);
    }
}
